package com.flame.workflow.ru.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.flame.auth.core.context.AuthContextHolder;
import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.workflow.ru.controller.vo.*;
import com.flame.workflow.ru.entity.Instance;
import com.flame.workflow.ru.service.InstanceService;
import com.flame.workflow.ru.service.UserTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * flame_workflow_ru_instance 流程实例表
 *
 * @author 吴欣童
 * @since 2023-06-05 14:27:43
 */
@RestController("workflow-instance-controller")
public class InstanceController {

    @Autowired
    private InstanceService instanceService;

    @Autowired
    private UserTaskService userTaskService;

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/start")
    public Result startInstance(@RequestBody InstanceStartVo instanceStartVo) throws JsonProcessingException {
        instanceService.startInstance(instanceStartVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/temp-submit")
    public Result tempSubmit(@RequestBody InstanceStartVo instanceStartVo) throws JsonProcessingException {
        instanceService.tempSubmit(instanceStartVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/temp-submit-maintain")
    public Result tempSubmitMaintain(@RequestBody TempInstanceMaintainVo tempInstanceMaintainVo)
            throws JsonProcessingException {
        instanceService.tempSubmitMaintain(tempInstanceMaintainVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/temp-submit-start")
    public Result tempSubmitStart(@RequestBody TempInstanceStartVo tempInstanceStartVo) {
        instanceService.tempInstanceStart(tempInstanceStartVo);
        return Result.success();
    }

    @PostMapping("/workflow/ru/instance")
    public Result getInstance(@RequestBody Instance instance) {
        return Result.success(instanceService.getInstance(instance.getInstanceId()));
    }

    @PostMapping("/workflow/ru/instance/page")
    public PageResult pageInstance(@RequestBody InstanceVo instanceVo) {
        instanceVo.setUserId(AuthContextHolder.getContext().getAccount());
        return PageResult.success(instanceService.pageInstance(instanceVo));
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/update")
    public Result updateInstance(@RequestBody @Validated Instance instance) {
        instanceService.updateInstance(instance);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/cancle")
    public Result cancelInstance(@RequestBody @Validated CancleInstanceVo cancleInstanceVo) {
        instanceService.cancelInstance(cancleInstanceVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/rollback-to-node")
    public Result rollbackToNode(@RequestBody @Validated RollbackToNodeVo rollbackToNodeVo) {
        instanceService.rollbackToNode(rollbackToNodeVo);
        return Result.success();
    }

    @PostMapping("/workflow/ru/user/task/page")
    public PageResult pageUserTask(@RequestBody UserTaskVo userTaskVo) {
        userTaskVo.setUserId(AuthContextHolder.getContext().getAccount());
        return PageResult.success(userTaskService.pageUserTask(userTaskVo));
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/task/approve")
    public Result approvalTask(@RequestBody ApproveVo approveVo) {
        instanceService.handleApproveTask(approveVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/instance/task/add-user")
    public Result taskAddUser(@RequestBody TaskAddUserVo taskAddUserVo) {
        instanceService.taskAddUser(taskAddUserVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/user/task/transfer")
    public Result transferUserTask(@RequestBody TransferUserTaskVo transferUserTaskVo) {
        instanceService.transferUserTask(transferUserTaskVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/workflow/ru/user/task/copy-send")
    public Result taskCopySend(@RequestBody TaskCopySendVo taskCopySendVo) {
        instanceService.taskCopySend(taskCopySendVo);
        return Result.success();
    }


    @PostMapping("/workflow/ru/trigger/list")
    public Result listTriggerTask() {
        return Result.success(instanceService.listTriggerTask());
    }
}
